package com.java47.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.util.StringUtil;
import com.java47.pojo.Item;
import com.java47.pojo.Option;
import com.java47.pojo.Subject;
import com.java47.pojo.Users;
import com.java47.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UsersController {

    private static int max = 1;
    @Resource
    private UserService us;
    private static String osid;

    @RequestMapping("login")
    public String login(String uname, String pwd, HttpSession session){
        Users user = us.selectOne(uname);
        if(!StringUtil.isEmpty(uname)&&user!=null&&pwd.equals(user.getPwd())){
            session.setAttribute("status",user.getStatus());
            session.setAttribute("uid",user.getUid());
            session.setAttribute("uname",uname);
            session.setAttribute("status",user.getStatus());
            return "forward:slist";
        }
        return "login";
    }

    @RequestMapping("slist")
    private String slist(Model model,String indexs,String title,HttpSession session,String weihu){
        if(indexs==null){
            indexs="1";
        }
        int index = Integer.parseInt(indexs);
        if(index<=0){
            index = 1;
        }else if(index>=max){
            index = max;
        }
        if("1".equals(weihu)){
            session.removeAttribute("weihu");
        }
        Page<Object> page =us.showPage(index,12,title);
        List<Subject> list = (List)page;
        for(int i=1;i<=list.size();i++){
            int js=0;
            int num = 0;
            if(i<=list.size()){
                for(Option o:list.get(i-1).getOption()){
                    js++;
                    num+=Integer.parseInt(o.getOrder());
                }
            }
            list.get(i-1).setJs(js);
            list.get(i-1).setSum(num);
        }
        max = page.getPages();
        model.addAttribute("title",title);
        model.addAttribute("slist",list);
        return "votelist";
    }

    @RequestMapping("toweihu")
    private String toweihu(HttpSession session){
        session.setAttribute("weihu","weihu");
        return "forward:slist";
    }

    @RequestMapping("del")
    private String del(String sid){
        System.out.println("===========先删除Item=============");
        Map<String,Object> imap = new HashMap<>();
        imap.put("delItem",sid);
        us.update(imap);
        System.out.println("===========再删除Option=============");
        Map<String,Object> omap = new HashMap<>();
        omap.put("delOption",sid);
        us.update(omap);
        System.out.println("===========最后删除Subject=============");
        Map<String,Object> smap = new HashMap<>();
        smap.put("delSubject",sid);
        us.update(smap);
        return "forward:slist";
    }

    @RequestMapping("toUpdate")
    private String toUpdate(){
        return "update";
    }

    @RequestMapping("update")
    private String update(String sid,Model model,HttpSession session){
        model.addAttribute("subject",us.showOne(sid));
        session.setAttribute("olist",us.showOlist(sid));
        osid=sid;
        return "update";
    }

    @RequestMapping("weihu")
    private String weihu(String[] option,String[] oid,HttpSession session){
        //获取保存好的List
        List<Option> o = (List<Option>)session.getAttribute("olist");
        //修改
        for(int i=0;i<option.length;i++){
            Map<String,Object> map = new HashMap<>();
            map.put("updateOption",new Option(oid[i],option[i],null,null,null));
            us.update(map);
        }
        //增加选项
        for(int i = 0;i<option.length;i++){
            if(oid[i].equals("")){
                Map<String,Object> map2 = new HashMap<>();
                map2.put("addOption",new Option(option[i],osid));
                us.update(map2);
            }
        }
        //排查
        for(int i=0;i<o.size();i++){
            for(int j=0;j<oid.length;j++){
                if(o.get(i).getOid().equals(oid[j])){
                    o.get(i).setOid(null);
                    break;
                }
            }
        }
        //删除选项
        for(int i =0;i<o.size();i++){
            //找到要删除的ID
            if(o.get(i).getOid()!=null){
                Map<String,Object> map1 = new HashMap<>();
                //先删除Item表里面外键关联的
                map1.put("deleteItem",new Item((String)session.getAttribute("uid"),osid,o.get(i).getOid()));
                if(us.update(map1)) System.err.println("删除1");
                Map<String,Object> map2 = new HashMap<>();
                //再按Oid删除Option表里面的
                map2.put("deleteOption",o.get(i).getOid());
                if(us.update(map2)) System.err.println("删除2");
            }
        }
        return "redirect:slist";
    }

    @RequestMapping("esc")
    private String esc(){
        return "login";
    }
}
